<?php
namespace app\houtai\controller\user\game\record;
use app\houtai\controller\BaseController;
use RuntimeException;

class TempController extends BaseController {
    /**
     *  @brief  : 临时战绩-列表
     *  @return : page
     *  @details:
     */
    public function indexAction() {
        $conds = [];
        $conds1 = [];
        $order = [];
        $CHANNELS = $this->GetAdminChannel();
        ////////////////////////////////////////////////////////////////////////////
        $Channel = $this->request->param('Channel/d', -1);
        if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
            $conds1['d.Channel'][] = ['IN', array_keys($CHANNELS)];
            $conds1['d.Channel'][] = ['EQ', $Channel];
        } else {
            $conds1['d.Channel'] = ['IN', array_keys($CHANNELS)];
        }
        /////////////////////////////////////////////////////////////////////////////////
        $search = $this->request->param('search/d', 0); // 房间号
        if ($search > 0) {
            $conds['a.RoomID'] = ['LIKE', "{$search}%"];
        }
        ////////////////////////////////////////////////////////////////////////////
        $orderMap = [
            'B' => 'Channel',
            'D' => 'RoomID',
            'E' => 'TotalPlay',
            'F' => 'TimeStart',
            'G' => 'TimeEnd',
        ];
        $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'F';
        $order[$orderMap[$this->orderField]] = $this->orderDirection;

        $sql2 = table('TempGameRecordData a', 'rydb.record')->field([
            'a.RoomID',
            'a.StartGameTime'   => 'GroupID',
            'COUNT(a.RecordID)' => 'TotalPlay',
            'MIN(a.InsertTime)' => 'TimeStart',
            'MAX(a.InsertTime)' => 'TimeEnd',
        ])->where($conds)->group('a.RoomID, a.StartGameTime')->buildSql();

        $list = table("{$sql2} b", 'rydb.record')->field([
            'd.Channel','c.ServerName', 'RoomID', 'GroupID',
            'TotalPlay', 'TimeStart', 'TimeEnd',
        ])
            ->join('CYBasicDB.dbo.GameRoomInfo c','c.ServerID=(LEFT(b.RoomID,2)-10)','LEFT')
            ->join('CYBasicDB.dbo.GameChannelRoom d','d.ServerID=c.ServerID','LEFT')->where($conds1)->order($order)->paginate();
        ////////////////////////////////////////////////////////////////////////////
        return $this->fetch('', [
            'list'  => $list,
            'search'=> empty($search) ? '' : $search,
            'Channel'   => $Channel,
            'CHANNELS'  => $CHANNELS,
        ]);

    }
    /**
     *  @brief  : 临时战绩-详情
     *  @return : page
     *  @details:
     */
    public function detailAction() {
        $listRecord = [];
        $listAvatar = [];
        ////////////////////////////////////////////////////////////////////////////
        $RoomID   = $this->request->param('RoomID/d', 0);
        $GroupID  = $this->request->param('GroupID/d', 0);
        if ($RoomID <= 0 || $GroupID <= 0) {
            return $this->error("查询参数无效");
        }
        $rows = table('TempGameRecordData a', 'rydb.record')
            ->where(['a.RoomID' => $RoomID, 'a.StartGameTime' => $GroupID])
            ->select();
        if (count($rows) > 0) {
            $GameIDs = [];
            foreach($rows as $row) {
                $data = iconv('CP936', 'UTF-8', trim($row['UserDate'])); // 'CP54936','CP936','MS936','WINDOWS-936'

                $matches = [];
                $eax = preg_match_all('/^RoomID:(\d+) Cnt:(\d+) NickName:(.+)\/ UserID:(.+)\/ Score:(.+)\/$/', $data, $matches, PREG_SET_ORDER);
                if ($eax == 1 && count($matches[0]) == 6) {
                    $GameID   = explode('/', $matches[0][4]);
                    $Scores   = explode('/', $matches[0][5], count($GameID));
                    $NickName = explode('/', $matches[0][3], count($GameID));
                    $DataRaw  = array_map(null, $GameID, $Scores, $NickName);
                    $listRecord[] = [
                        'RoomID' => $matches[0][1],
                        'Time'   => $row['InsertTime'],
                        'Cnt'    => $matches[0][2],
                        'DataRaw'=> $DataRaw,
                    ];
                    foreach($GameID as $val) {
                        $GameIDs[$val] = 1;
                    }
                } else {
                    throw new RuntimeException('UserDate解析失败');
                }
            }
            $GameIDs = array_keys($GameIDs);
            $listAvatar = table('AccountsInfo a', 'rydb.basic')->field([
                'a.UserID', 'a.GameID', "ISNULL(a.AccountImgUrl, '')" => 'Avatar',
            ])
                ->where(['a.GameID'=>['IN', $GameIDs]])->select();
        }
        ////////////////////////////////////////////////////////////////////////////
        return $this->fetch('', [
            'listRecord' => $listRecord,
            'listAvatar' => $listAvatar,
        ]);
    }
}
